//
//  MapViewController.m
//  Express
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 FJY. All rights reserved.
//

#import "MapViewController.h"

@interface MapViewController ()<MKMapViewDelegate>

@property (nonatomic, strong) MKMapView *mapView;

@end

@implementation MapViewController

- (NSMutableArray *)nearbyInfoArray {
    if (!_nearbyInfoArray) {
        _nearbyInfoArray = [[NSMutableArray alloc] init];
    }
    return _nearbyInfoArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.
    mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.mapView.delegate = self;
    [_mapView setMapType:MKMapTypeStandard];
    [_mapView setZoomEnabled:YES];
    [_mapView setScrollEnabled:YES];
    [_mapView setRotateEnabled:NO];

    _mapView.showsUserLocation = YES;
    
    //用户位置追踪(用户位置追踪用于标记用户当前位置，此时会调用定位服务)
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    //地图中心设置
    [self.mapView setCenterCoordinate:self.currentCoordinate];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(self.currentCoordinate.latitude , self.currentCoordinate.longitude), 3000, 3000);
    [_mapView setRegion:[_mapView regionThatFits:region]];

    
    [self.view addSubview:_mapView];

    
    
}

//地图将要载入
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView {
    if (_nearbyInfoArray.count != 0) {
        for (MKMapItem *mapItem in _nearbyInfoArray) {
            MKPointAnnotation *newPointAnnotation = [[MKPointAnnotation alloc] init];
            newPointAnnotation.title = mapItem.name;
            newPointAnnotation.subtitle = mapItem.phoneNumber;
            newPointAnnotation.coordinate = mapItem.placemark.coordinate;
            [_mapView addAnnotation:newPointAnnotation];
        }
    }
    if (_destinationMapItem != nil) {
        //设置导航始、终点
        CLLocationCoordinate2D fromcoor = self.currentCoordinate;
        CLLocationCoordinate2D tocoor = self.destinationMapItem.placemark.coordinate;
        
        //创建出发点和目的点信息
        MKPlacemark *fromPlace = [[MKPlacemark alloc] initWithCoordinate:fromcoor addressDictionary:nil];
        
        MKPlacemark *toPlace = [[MKPlacemark alloc]initWithCoordinate:tocoor addressDictionary:nil];
        
        //创建出发节点和目的地节点
        MKMapItem * fromItem = [[MKMapItem alloc]initWithPlacemark:fromPlace];
        MKMapItem * toItem = [[MKMapItem alloc]initWithPlacemark:toPlace];
        
        //初始化导航搜索请求
        MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
        request.source = [MKMapItem mapItemForCurrentLocation];
        request.destination = toItem;
        request.requestsAlternateRoutes=YES;
        
        //初始化请求检索
        MKDirections *directions = [[MKDirections alloc]initWithRequest:request];
        
        //开始检索，结果会返回在block中
        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
            if (error) {
                NSLog(@"error:%@",error);
            }else{
                
                //提取导航线路结果中的一条线路
                MKRoute *route =response.routes[0];
                
                //将线路中的每一步详情提取出来
                NSMutableArray *stepArray = [NSMutableArray arrayWithArray:route.steps];
                //进行遍历
                for (int i = 0; i < stepArray.count; i++) {
                    
                    //线路的详情节点
                    MKRouteStep * step = stepArray[i];
                    
                    //在此节点处添加一个大头针
                    MKPointAnnotation * point = [[MKPointAnnotation alloc]init];
                    point.coordinate=step.polyline.coordinate;
                    point.title=step.instructions;
                    point.subtitle=step.notice;
                    [mapView addAnnotation:point];
                    
                    //将此段线路添加到地图上
                    [mapView addOverlay:step.polyline];
                    
                }
            }
        }];
    }
}


- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
    
}

//地图显示区域将要发生改变
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
    
}

//地图加载失败
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error {
    NSLog(@"error:%@",[error description]);
}

//地图覆盖物的代理方法
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
    
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay]; 
    renderer.strokeColor = [UIColor redColor];
    renderer.lineWidth = 4.0;
    return  renderer;
    
}

//用户位置跟踪
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

}

//标注的代理方法
//- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
//    MKPinAnnotationView * view= [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"anno"];
//    return view;
//}



- (void)viewDidDisappear:(BOOL)animated {
    [self.nearbyInfoArray removeAllObjects];
    self.destinationMapItem = nil;
    [self.mapView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
